package com.sc.web.acc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sc.dal.common.pagination.Page;
import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.acc.AccounttypeMapper;
import com.sc.dal.model.acc.Accounttype;
import com.sc.service.acc.AcctypeService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;

/**
 * 测试例子控制器
 * @author Administrator
 *
 */
@Controller
public class AccTypeController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AcctypeService acctypeService;
	
	
	/**
	 * 测试方法
	 * @param request
	 * @param blank
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "test")
	public ModelAndView test(HttpServletRequest request,String blank,HttpSession session) {
		ModelAndView modelView = new ModelAndView();
		Long id = 1l;
		try{
			Accounttype act = acctypeService.selectByPrimaryKey(id);
			if(act==null){
				logger.warn("没有查询到数据!");
				modelView.addObject("acctype", "没有查到数据!");
			}else{
				modelView.addObject("acctype", act.getName()+act.getRemark());
			}
			modelView.setViewName("/test");
		}catch(Exception e){
			logger.error("查询accounttype表出现异常,账号类型主键:"+id,e);
			throw new ScBizException("查询accounttype表出现异常,查询参数:"+id);
		}
		return modelView;
	}
	
	/**
	 * 测试方法返回json
	 * @param request
	 * @param blank
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "testbody")
	@ResponseBody
	public Map<String,Object> test2(HttpServletRequest request,String blank,HttpSession session) {
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		Long id = 1l;
		try{
			Accounttype act = acctypeService.selectByPrimaryKey(id);
			if(act==null){
				logger.warn("没有查询到数据!");
				resultMap.put("acctype", "没有查到数据!");
				
			}else{
				resultMap.put("acctype", act.getName()+act.getRemark());
			}
		}catch(Exception e){
			logger.error("查询accounttype表出现异常,账号类型主键:"+id,e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "testpage")
	@ResponseBody
	public Map<String,Object> selectPagination(){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Page page = new Page();
		//传入页号
		page.setCurrPage(1);
		//传入每页记录数
		page.setPageSize(10);
		Accounttype  acctype = new Accounttype();
		//添加条件
		//acctype.setName("aa");
		List<Accounttype> resultList = new ArrayList<Accounttype>();
		int totalRecords = 0;
		int totalPage = 0;
		try {
			PaginationBean<Accounttype> result = acctypeService.getPagingDatas(acctype, page, "count", "selectByParam",AccounttypeMapper.class);
			resultList = result.getPageList();
			totalRecords = result.getTotalRecords();
			totalPage = result.getTotalPage();
			
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		} catch (Exception e) {
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("分页查询流水账表异常！");
			throw new ScBizException("分页查询流水账表异常！", e);
		}	
		resultMap.put("items", resultList);
		resultMap.put("count", totalRecords);
		resultMap.put("pages", totalPage);

		return resultMap;
	}
}
